package com.yckj.activechicken.business.store.controller;

import com.yckj.activechicken.business.store.model.StorehouseEntity;
import com.yckj.activechicken.business.store.model.StorehouseInfo;
import com.yckj.activechicken.business.store.model.vo.StoreHouseVo;
import com.yckj.activechicken.business.store.service.IStoreHouseService;
import com.yckj.activechicken.common.constant.StoreConstants;
import com.yckj.activechicken.common.utils.security.SecurityUtils;
import com.yckj.activechicken.framework.web.controller.BaseController;
import com.yckj.activechicken.framework.web.domain.AjaxResult;
import com.yckj.activechicken.framework.web.page.TableDataInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title:com.yckj.activechicken.business.store.controller
 * </p>
 * <p/>
 * <p>
 * Description: 描述【仓库管理控制器】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/6/11 15:49
 */
@RestController
@RequestMapping("/store")
public class StoreHouseController extends BaseController{

    @Autowired
    private IStoreHouseService storeHouseService;

    /**
     * 添加实体仓库
     * @param storeHouseVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('store:addStore')")
    @PostMapping("/addStore")
    public AjaxResult addStore(@RequestBody StoreHouseVo storeHouseVo) {
        String username = SecurityUtils.getUsername();
        Date newDate = new Date();
        storeHouseVo.setCreateBy(username);
        storeHouseVo.setCreateTime(newDate);
        storeHouseVo.setUpdateBy(username);
        storeHouseVo.setUpdateTime(newDate);

        StorehouseInfo storehouseInfo = new StorehouseInfo();
        StorehouseEntity storehouseEntity = new StorehouseEntity();
        BeanUtils.copyProperties(storeHouseVo,storehouseInfo);
        BeanUtils.copyProperties(storeHouseVo,storehouseEntity);

        storehouseInfo.setStoreCategory(StoreConstants.STORE_CATEGORY_COMPANY);
        storehouseInfo.setStoreType(StoreConstants.STORE_TYPE_STOCK);

        Map<String,String> outMap = storeHouseService.addStore(storehouseInfo,storehouseEntity);

        String result = outMap.get("result");
        if("true".equals(result)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error(outMap.get("message"));
        }
    }

    /**
     * 多条件分页查询仓库信息
     * @param storeHouseVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('store:listStore')")
    @GetMapping("/listStore")
    public TableDataInfo listStore(StoreHouseVo storeHouseVo) {
        startPage();
        List<StoreHouseVo> list = storeHouseService.listStore(storeHouseVo);
        return getDataTable(list);
    }

    /**
     * 修改仓库信息
     * @param storeHouseVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('store:modifyStore')")
    @PostMapping("/modifyStore")
    public AjaxResult modifyStore(@RequestBody StoreHouseVo storeHouseVo) {
        String username = SecurityUtils.getUsername();
        Date newDate = new Date();
        storeHouseVo.setUpdateBy(username);
        storeHouseVo.setUpdateTime(newDate);

        StorehouseInfo storehouseInfo = new StorehouseInfo();
        StorehouseEntity storehouseEntity = new StorehouseEntity();
        BeanUtils.copyProperties(storeHouseVo,storehouseInfo);
        BeanUtils.copyProperties(storeHouseVo,storehouseEntity);

        Map<String,String> outMap = storeHouseService.modifyStore(storehouseInfo,storehouseEntity);

        String result = outMap.get("result");
        if("true".equals(result)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error(outMap.get("message"));
        }
    }

    /**
     * 删除仓库信息
     * @param storehouseInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('store:delStore')")
    @PostMapping("/delStore")
    public AjaxResult delStore(@RequestBody StorehouseInfo storehouseInfo) {
        String username = SecurityUtils.getUsername();
        Date newDate = new Date();
        storehouseInfo.setUpdateBy(username);
        storehouseInfo.setUpdateTime(newDate);

        return toAjax(storeHouseService.delStore(storehouseInfo));
    }
}
